from django.db import models


# Create your models here.


class UserInfo(models.Model):
    """用户信息"""
    username = models.CharField(verbose_name='用户名', max_length=32)
    password = models.CharField(verbose_name='密码', max_length=32)
    email = models.EmailField(verbose_name='邮箱', max_length=32)
    mobile_phone = models.CharField(verbose_name='手机号', max_length=32)

    def __str__(self):
        return self.username

class Good(models.Model):
    type_choices = (
        (1, '生活用品'),
        (2, '食品')
    )
    name = models.CharField(verbose_name='商品名称', max_length=256)
    Good_ID = models.CharField(verbose_name='商品ID', max_length=32, unique=True)
    stock = models.PositiveIntegerField(verbose_name='商品库存', default=0)
    stars = models.PositiveIntegerField(verbose_name='商品收藏')
    views = models.PositiveIntegerField(verbose_name='商品浏览量')
    desc = models.TextField(verbose_name='商品介绍')
    owner = models.ForeignKey(verbose_name='所有者', to='UserInfo', on_delete=models.CASCADE)
    image = models.ImageField(upload_to='commodity/')
    buy = models.PositiveIntegerField(verbose_name='商品出售量')
    type = models.SmallIntegerField(verbose_name='商品类别', default=1, choices=type_choices)

class Transaction(models.Model):
    type_choices = (
        (1, '入库'),
        (2, '出售')
    )
    time = models.DateTimeField(verbose_name='时间', auto_now_add=True)
    Tran_ID = models.CharField(verbose_name='记录ID', max_length=32, unique=True)
    good = models.ForeignKey(verbose_name='商品', to='Good', on_delete=models.CASCADE)
    quantity = models.PositiveIntegerField(verbose_name='数量')
    operator = models.ForeignKey(verbose_name='操作人', to='UserInfo', on_delete=models.CASCADE, related_name='operated_transactions')
    type = models.SmallIntegerField(verbose_name='操作类型', default=1, choices=type_choices)
    owner = models.ForeignKey(verbose_name='所有者', to='UserInfo', on_delete=models.CASCADE, related_name='owned_transactions')
